#!/usr/bin/env python
#
# Copyright 2011 kokorohamoe@gmail.com
#


from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import urllib
import re
import hashlib
import datetime
from google.appengine.ext import db
from google.appengine.api import users
from simpleoauth_gae.twitter import OAuthApi
import twitter_key

consumer_key = twitter_key.getConsumerKey()   # your consumer key
consumer_secret = twitter_key.getConsumerSecret()   # your consumer secrety

class MainHandler(webapp.RequestHandler):
    def get(self,a):
        self.response.out.write('<html>\n')		        		
 	self.response.out.write('\t<body>\n')
	self.response.out.write('<a href="')
	twitter = OAuthApi(consumer_key, consumer_secret);
	temp_credentials = twitter.getRequestToken();
	self.response.out.write(twitter.getAuthorizationURL(temp_credentials));
        self.response.out.write('\t">Login</a>\n')
	self.response.out.write('</body>\n')
        self.response.out.write('</html>\n')

class CallBackHandler(webapp.RequestHandler):
    def get(self,a):
        self.response.out.write('<html>\n')
        self.response.out.write('\t<body>\n')
        self.response.out.write('<a href="')
        twitter = OAuthApi(consumer_key, consumer_secret);
        temp_credentials = twitter.getRequestToken();
        self.response.out.write(twitter.getAuthorizationURL(temp_credentials));
        self.response.out.write('\t">Login</a></body>\n')
        self.response.out.write('</html>\n')


def main():
    application = webapp.WSGIApplication([
				     (r'/twitter/callback/(.*)', CallBackHandler), 
				     (r'/twitter/(.*)', MainHandler),
                                     ],
                                     debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
